Function

  •  Function is a series of instruction/commands. Function performs particular activity in shell i.e. it had specific work to do or simply say task.
  • When program gets complex we need to use divide and conquer technique. It means whenever programs gets complicated, we divide it into small chunks/entities which are known as functions.

Example-1

#!/bin/sh

SayHello()  /* Define your function here */

{

   echo "Hello Selva, Have a nice day"

   return

}

SayHello  /* Invoking Function here */

outPut:-

Hello Selva, Have a nice day.


Example-2

#!/bin/sh

hello()

{

   echo "HELLO Selva GOOD MORNING"

   return

}

hai()

{

   echo "HAI Selva"

   return

}

hai

hello

exit

hai

In the above example, the function “hai” has been called again after the “exit” statement. So, script did not execute the “hai” again. Whatever the function/commands called after the exit statement will not be executed.

No comments:

Post a Comment